import 'package:flutter/material.dart';
import 'package:flutter_app/common/dto/dto.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_app/common/utils/screen.dart';
import 'package:flutter_app/pages/main/widgets/categories_widget.dart';
import 'package:flutter_app/pages/main/widgets/channels_widget.dart';
import 'package:flutter_app/pages/main/widgets/news_item_widget.dart';
import 'package:flutter_app/pages/main/widgets/newsletter_widget.dart';
import 'package:flutter_app/pages/main/widgets/recommend_widget.dart';
import 'package:get/get.dart';
import 'package:getwidget/components/shimmer/gf_shimmer.dart';

import 'logic.dart';
import 'state.dart';

class MainPage extends StatelessWidget {

  final MainLogic logic = Get.put(MainLogic());
  final MainState state = Get.find<MainLogic>().state;

  // 分类菜单
  Widget _buildCategories() {
    return Obx(() => state.categories.isEmpty == true ? Container() : newsCategoriesWidget(
      categories: state.categories,
      selCategoryCode: state.selCategoryCode.value,
      onTap: (CategoryResponse item) {
        logic.loadNewsData(item.code);
      },
    ));
  }
  // 抽取前先实现业务

  // 推荐阅读
  Widget _buildRecommend() {
    return Obx(() => state.newsRecommend.value.id == null // 此处判断的依据应该是code 数据没到位，可以用骨架图展示
        ? Container()
        : recommendWidget(state.newsRecommend.value)
    );
  }

  // 频道
  Widget _buildChannels() {
    return Obx(() => state.channels.isEmpty == true ? Container() : newsChannelsWidget(
      channels: state.channels,
      onTap: (ChannelResponse item) {},
    ));
  }

  // 新闻列表
  Widget _buildNewsList() {
    return Obx(() => state.newsPageList.value.counts == null ? Container(height: duSetHeight(161 * 5 + 100.0),) : Column(
      children: state.newsPageList.value.items!.map((item) {
        return Column(
          children: <Widget>[
            newsItem(item),
            Divider(height: 1),
          ],
        );
      }).toList(),
    ));
  }

  // ad 广告条
  // 邮件订阅
  Widget _buildEmailSubscribe() {
    return newsletterWidget();
  }


  @override
  Widget build(BuildContext context) {

    List<Widget> skeletonScreen = [];


    for(int i = 0; i < 8; i++){
      skeletonScreen.add(Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 54,
            height: 46,
            color: Colors.white,
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: double.infinity,
                  height: 8,
                  color: Colors.white,
                ),
                const SizedBox(height: 6),
                Container(
                  width: MediaQuery.of(context).size.width * 0.5,
                  height: 8,
                  color: Colors.white,
                ),
                const SizedBox(height: 6),
                Container(
                  width: MediaQuery.of(context).size.width * 0.25,
                  height: 8,
                  color: Colors.white,
                ),
              ],
            ),
          )
        ],
      ));
      skeletonScreen.add(Divider(height: 10),);
    }

    return Container(
      child: Text("我是主页"),
    );

    return Obx(() => state.newsPageList.value.counts == null ?
    /// 骨架屏幕
    GFShimmer(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: ListView(
          children: [
            ...skeletonScreen
          ],
        ),
      ),
    ):
    ///下拉刷新插件
    EasyRefresh(
      enableControlFinishRefresh: true,
      controller: state.easyRefreshController,
      header: ClassicalHeader(),
      onRefresh: logic.onRefresh,
      child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            _buildCategories(),
            Divider(height: 1),
            _buildRecommend(),
            Divider(height: 1),
            _buildChannels(),
            Divider(height: 1),
            _buildNewsList(),
            Divider(height: 1),
            _buildEmailSubscribe(),
          ],
        ),
      ),
    )

    );

  }
}
